package uk.ac.ncl.carrent.car;


import uk.ac.ncl.carrent.*;
import java.util.*;


public abstract class RentFactory implements Rent{
	
	private List<Car> smallCars = new ArrayList<Car>();
	private List<Car> largeCars = new ArrayList<Car>();
	private Map<Person, Car> rentedCars = new HashMap<Person, Car>();
	private 
	
	RentFactory(){
		
	}

	public int avaiableCars(String typeOfCar){
		
		if(typeOfCar.equals("small")){
			return smallCars.size();
		}
		else if(typeOfCar.equals("large")){
			return largeCars.size();
		}
		else
			return 0;		
	}
	
	public void addCars(RegistrationNumber number, String type){
		if(type.equals("small")){
			if(smallCars.size() < 20){
				smallCars.add(new SmallCar(number));
			}
			else{
				System.out.println("reached the limit number of the small cars");
			}
		}
		else if(type.equals("large")){
			if(largeCars.size() < 10){
				largeCars.add(new LargeCar(number));
			}
			else{
				System.out.println("reached the limit number of the large cars");
			}
		}
		else{
			throw new IllegalArgumentException("invalid car type");
		}
	}
	
	public Collection<Car> getRentedCars(){
		Collection<Car> rented = new ArrayList<Car>();
		for(Person person : rentedCars.keySet()){
			rented.add(rentedCars.get(person));
		}
		return rented;	
	}

	public Car getCar(DrivingLicence licence){
		Person person = licence.getPerson();
		if(rentedCars.containsKey(person)){
			Car car = rentedCars.get(person);
			return car;		
		}
		else{
			System.out.println(licence.getName() + " hasn't rented a car.");
			return null;
		}
	}

	public Car issueCar(DrivingLicence licence, String typeOfCar){	
		if(typeOfCar.equals("small")){
			if(smallCars.isEmpty()){
				System.out.println("There is no small car avaible to rent.");
				return null;
			}
			else if(licence.getType() == true 
					&& !rentedCars.containsKey(licence.getPerson()) 
					&& licence.getPerson().getAge() >= 20 
					&& licence.getYear() >= 1){
				Car car = smallCars.remove(0);
				car.setStatus(true);
				int fuel = car.fuelToAdd(car.getCapacity());
				System.out.println("The car's tank needs to be filled by " + fuel + "Litres");
				rentedCars.put(licence.getPerson(), car);
				return car;
			}
			else{
				System.out.println("The car can't be issued, please checked the requirements to rent a car.");
				return null;
			}
		}
		else if(typeOfCar.equals("large")){
			if(largeCars.isEmpty()){
				System.out.println("There is no large car avaible to rent.");
				return null;
			}
			else if(licence.getType() == true 
					&& !rentedCars.containsKey(licence.getPerson()) 
					&& licence.getPerson().getAge() >= 25 
					&& licence.getYear() >= 5){
				Car car = largeCars.remove(0);
				car.setStatus(true);
				int fuel = car.fuelToAdd(car.getCapacity());
				System.out.println("The car's tank needs to be filled by " + fuel + "Litres");
				rentedCars.put(licence.getPerson(), car);
				return car;
			}
			else{
				System.out.println("The car can't be issued, please checked the requirements to rent a car.");
				return null;
			}
		}
		else
			return null;			
	}
	
	public int terminateRental(DrivingLicence licence){
		if(rentedCars.containsKey(licence.getPerson())){
		Car car = rentedCars.remove(licence.getPerson());
		car.setStatus(false);
		int fuel = car.getCapacity() - car.getCurrentFuel();
		if(car.getCarType().equals("small")){
			smallCars.add(car);
		}
		else if(car.getCarType().equals("large")){
			largeCars.add(car);
		}
		return fuel;}
		else{
			System.out.println(licence.getName() + " hasn't rented car.");
			return 0;
		}
	}
}
